$(function () {
    $(".select2").select2();
    var district_id = $("#district_id").select2();
    var city_select2 = $("#city_id").select2();

    $("#province_id").on("select2:select", function(e){
        console.log($(this).val());
        var p_id = $(this).val();
        // area_type.val(1).trigger("change");
        if (p_id > 0) {
            // area_type.val(2).trigger("change");
            $.ajax({
                method: "GET",
                url: ajaxurl,
                data: {p_id:p_id}
            }).done(function(data){
                // console.log(data);
                $("#city_id").html(data);
                city_select2.val(0).trigger("change");
            })
        } else {
            $("#city_id").html('<option selected="selected" value="0">可选择</option>');
            city_select2.val(0).trigger("change");
        }
    });

    $("#city_id").on("select2:select", function(e){
        console.log($(this).val());
        var p_id = $(this).val();
        // area_type.val(1).trigger("change");
        if (p_id > 0) {
            // area_type.val(2).trigger("change");
            $.ajax({
                method: "GET",
                url: ajaxurl,
                data: {p_id:p_id}
            }).done(function(data){
                // console.log(data);
                $("#district_id").html(data);
                district_id.val(0).trigger("change");
            })
        } else {
            reset_district();
        }
    });
    function reset_district() {
        $("#district_id").html('<option selected="selected" value="0">可选择</option>');
        district_id.val(0).trigger("change");
    }
});

